package vn.co.thn.web.hotvideo.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import vn.co.thn.web.hotvideo.common.BeanBase;
import vn.co.thn.web.hotvideo.common.ControllerBase;
import vn.co.thn.web.hotvideo.common.Helper;
import vn.co.thn.web.hotvideo.data.jpa.Video;
import vn.co.thn.web.hotvideo.model.CategoryModel;
import vn.co.thn.web.hotvideo.model.VideoModel;

@SuppressWarnings("serial")
public class AdminVideo extends ControllerBase {
	private VideoModel videoModel = new VideoModel();
	private CategoryModel categoryMode = new CategoryModel();

	@Override
	protected void Get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String query = request.getQueryString();
		if (query != null && query.trim().length() > 0) {
			if (request.getParameter("action") != null) {
				if (request.getParameter("action").equalsIgnoreCase("new")) {
					request.setAttribute("categorylist", categoryMode.getList());
					request.setAttribute("action", 1);
					Forward("admin_video");
					return;
				}
				if (request.getParameter("action").equalsIgnoreCase("edit")) {
					request.setAttribute("data", videoModel.getObject(request.getParameter("Embed_code")));
					request.setAttribute("categorylist", categoryMode.getList());
					request.setAttribute("action", 2);
					Forward("admin_video");
					return;
				}
				if (request.getParameter("action").equalsIgnoreCase("delete")) {
					videoModel.Delete(request.getParameter("Embed_code"));
					response.setContentType("text/html;charset=UTF-8");
					PrintWriter out = response.getWriter();
					List<Video> lst = videoModel.getList();
					if (lst.size() > 0) {
						for (Video obj : lst) {
							out.println("<div class=\"content_item\">");
							out.println("<div id=\"title\">" + obj.getTitle() + "</div>");
							out.println("<img src=\"" + obj.getImage_Url() + "\" />");
							out.println("<div>");
							out.println("<a href=\"?action=edit&Embed_code=" + obj.getEmbed_code() + "\">edit</a>");
							out.println("<a href=\"JavaScript:ajax('?action=delete&Embed_code=" + obj.getEmbed_code() + "','htmldata')\">delete</a>");
							out.println("</div>");
							out.println("</div>");
						}
					} else {
						out.println("");
					}
					return;
				}
			}
		} else {
			request.setAttribute("datalist", videoModel.getList());
			request.setAttribute("action", 0);
			Forward("admin_video");
		}
	}

	@Override
	protected void Post(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Video video = BeanBase.RequestToObject(request, Video.class);
		video.setImage_Url("http://img.youtube.com/vi/" + video.getEmbed_code() + "/0.jpg");
		video.setDate_Update(Helper.CurrentDateTime());
		video.setContent_Search(video.getTitle() +" " +video.getContent().getValue());
		videoModel.Add(video);
		Redirect("/admin/video");
	}

}
